package tasksimulator;

import java.sql.SQLException;

import cassandra.client.CassandraClient;
import vs.database.M_DB;
import vs.database.M_Query;

/**
 *
 * @author Jonas
 */
public class Databaser {

    public enum DB {

        POSTGRES, CASSANDRA
    };
    public static DB database;
    private static M_DB db = null;
    public static String PGHOST = "146.164.98.162";

    private static void connectDatabase() {
        String conexao = "jdbc:postgresql://" + PGHOST + "/tasksimulator?chartset=UTF8";
        db = new M_DB(M_DB.DRIVER_POSTGRESQL, conexao, "postgres", "postgres", true);
    }

    private static void storeOnPostgres(Task task) throws SQLException {
        if (db == null) {
            connectDatabase();
        }

        db.beginTransaction();
        String query = "INSERT INTO tasks(key, activityid, processor, "
                + "commandline, workspace, start, status)"
                + "VALUES(?,?,?,?,?,?,?)";

        M_Query mquery = db.prepQuery(query);
        mquery.setParInt(1, (int) task.key);
        mquery.setParInt(2, (int) task.activityId);
        mquery.setParInt(3, task.processor);
        mquery.setParString(4, task.commandLine);
        mquery.setParString(5, task.workspace);
        mquery.setParDate(6, task.start);
        mquery.setParString(7, task.status);

        mquery.executeUpdate();
        db.commitTransaction();
    }

    private static void storeOnCassandra(Task task) {
    	CassandraClient client = new CassandraClient();
    	client.insertColumn("Chiron", "Activations", task.key+"", "activityId", task.activityId+"");
    	client.insertColumn("Chiron", "Activations", task.key+"", "processor", task.processor+"");
    	client.insertColumn("Chiron", "Activations", task.key+"", "commandLine", task.commandLine+"");
    	client.insertColumn("Chiron", "Activations", task.key+"", "workspace", task.workspace+"");
    	client.insertColumn("Chiron", "Activations", task.key+"", "start", task.start+"");
    	client.insertColumn("Chiron", "Activations", task.key+"", "status", task.status+"");
        
    	client.close();
    }

    private static void updateOnPostgres(Task task) throws SQLException {
        if (db == null) {
            connectDatabase();
        }
        db.beginTransaction();
        String query = "UPDATE tasks SET exitstatus = ? , error = ? , output = ? , task_end = ?, status = ? WHERE key = ?";

        M_Query mquery = db.prepQuery(query);
        mquery.setParInt(1, (int) task.exitStatus);
        mquery.setParString(2, task.error);
        mquery.setParString(3, task.output);
        mquery.setParDate(4, task.end);
        mquery.setParString(5, task.status);
        mquery.setParInt(6, (int) task.key);

        mquery.executeUpdate();
        db.commitTransaction();
    }

    private static void updateOnCassandra(Task task) {
    	CassandraClient client = new CassandraClient();
    	client.insertColumn("Chiron", "Activations", task.key+"", "exitStatus", task.exitStatus+"");
    	client.insertColumn("Chiron", "Activations", task.key+"", "error", task.error+"");
    	client.insertColumn("Chiron", "Activations", task.key+"", "output", task.output+"");
    	client.insertColumn("Chiron", "Activations", task.key+"", "end", task.end+"");
    	client.insertColumn("Chiron", "Activations", task.key+"", "status", task.status+"");
    	
    	client.close();
    }

    public static void storeTask(Task task) throws SQLException {
        if (database.equals(DB.POSTGRES)) {
            storeOnPostgres(task);
        } else {
            storeOnCassandra(task);
        }
    }

    public static void updateTask(Task task) throws SQLException {
        if (database.equals(DB.POSTGRES)) {
            updateOnPostgres(task);
        } else {
            updateOnCassandra(task);
        }
    }
}
